package customWidgets;
import hero.main.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HeroAdapter extends ArrayAdapter<Hero> {

	Context context; 
    int layoutResourceId;    
    Hero data[] = null;
    
    public HeroAdapter(Context context, int layoutResourceId, Hero[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
      
        HeroHolder holder = new HeroHolder();
        
        if(convertView == null)
        {
        	
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(layoutResourceId, null);
            holder.imgIcon = (ImageView) convertView.findViewById(R.id.imageView1);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.textView2);
            convertView.setTag(holder);
        }
        else
        {
            holder = (HeroHolder)convertView.getTag();
        }
        
        Hero hero = data[position];
       
        holder.txtTitle.setText(hero.title);
        holder.imgIcon.setImageResource(hero.icon);
        
        return convertView;
    }
    
    static class HeroHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
      
    }
}
